package com.adv.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.adv.mapper.MessageMapper;
import com.adv.model.Message;
import com.adv.model.MessageSqlParam;
import com.adv.util.Constant;
import com.adv.util.DozerUtil;
import com.adv.vo.MessageForm;

@Component
public class MessageService {

	@Autowired
	private MessageMapper messageMapper;

	public List<Message> getMessage() {
		return messageMapper.selectByParam(new MessageSqlParam());
	}

	public Message getMessageByPrimaryId(Integer id) {
		return messageMapper.selectByPrimaryKey(id);
	}

	// 新建和保存消息
	public Map<String, Object> saveMessage(MessageForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form) {
			if (StringUtils.isEmpty(form.getRegistertime())) {
				form.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			}
			Message record = DozerUtil.mapObj(form, Message.class);
			if (null != record.getId()) {
				int num = messageMapper.updateByPrimaryKeySelective(record);
				if (num == 1) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			} else {
				int num = messageMapper.insertSelective(record);
				if (num == 1) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			}
		}
		return resultMap;
	}

	// 修改锁屏消息
	public Map<String, Object> editAdvertiser(MessageForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			Message record = DozerUtil.mapObj(form, Message.class);
			int num = messageMapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
			}
		}
		return resultMap;
	}

	public long getCountBySelective(MessageSqlParam param) {
		return messageMapper.countByParam(param);
	}

	// 删除消息
	public Map<String, Object> deleteMessage(MessageForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			int num = messageMapper.deleteByPrimaryKey(form.getId());
			if (num == 1) {
				resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
			}
		}
		return resultMap;
	}
}